#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->horizontalSlider->setMinimum(200);
    ui->horizontalSlider->setMaximum(1000);
    ui->horizontalSlider->setValue(500);
    ui->horizontalSlider->setSingleStep(50);

    ui->verticalSlider->setMinimum(200);
    ui->verticalSlider->setMaximum(1000);
    ui->verticalSlider->setValue(500);
    ui->verticalSlider->setSingleStep(50);

    QShortcut* shortcut1 = new QShortcut(this);
    shortcut1->setKey(QKeySequence("-"));
    connect(shortcut1,&QShortcut::activated,this,&Widget::subValue);

    QShortcut* shortcut2 = new QShortcut(this);
    shortcut2->setKey(QKeySequence("="));
    connect(shortcut2,&QShortcut::activated,this,&Widget::addValue);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::subValue()
{
    int vValue = ui->verticalSlider->value()-50;
    int hValue = ui->horizontalSlider->value()-50;
    if(vValue >= ui->verticalSlider->minimum()&& vValue <= ui->verticalSlider->maximum())
    {
        ui->verticalSlider->setValue(vValue);
    }
    if(hValue >= ui->horizontalSlider->minimum()&& hValue <= ui->horizontalSlider->maximum())
    {
        ui->horizontalSlider->setValue(hValue);
    }
}

void Widget::addValue()
{
    int vValue = ui->verticalSlider->value()+50;
    int hValue = ui->horizontalSlider->value()+50;
    if(vValue >= ui->verticalSlider->minimum()&& vValue <= ui->verticalSlider->maximum())
    {
        ui->verticalSlider->setValue(vValue);
    }
    if(hValue >= ui->horizontalSlider->minimum()&& hValue <= ui->horizontalSlider->maximum())
    {
        ui->horizontalSlider->setValue(hValue);
    }
}


void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText("当前值为: " + QString::number(value));

    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(),rect.y(),value,rect.height());
}

void Widget::on_verticalSlider_valueChanged(int value)
{
    ui->label_2->setText("当前值为: " + QString::number(value));

    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(),rect.y(),rect.width(),value);
}
